using Microsoft.Extensions.Configuration;

namespace Terra.Core.Constants;

/// <summary>
/// 项目常量
/// </summary>
public static class ProjectConstants
{
    /// <summary>
    /// 获取项目名称
    /// </summary>
    public static string Name { get; private set; } = "Terra";

    /// <summary>
    /// 获取项目显示名称
    /// </summary>
    public static string DisplayName { get; private set; } = "Terra Framework";

    /// <summary>
    /// 获取数据库表前缀
    /// </summary>
    public static string TablePrefix { get; private set; } = "terra_";

    /// <summary>
    /// 获取缓存键前缀
    /// </summary>
    public static string CachePrefix { get; private set; } = "terra:";

    /// <summary>
    /// 获取消息队列前缀
    /// </summary>
    public static string QueuePrefix { get; private set; } = "terra.";

    /// <summary>
    /// 初始化项目常量
    /// </summary>
    /// <param name="configuration">配置</param>
    public static void Initialize(IConfiguration configuration)
    {
        var settings = configuration.GetSection("ProjectSettings").Get<Configuration.ProjectSettings>();
        if (settings != null)
        {
            Name = settings.Name;
            DisplayName = settings.DisplayName;
            TablePrefix = settings.TablePrefix;
            CachePrefix = settings.CacheKeyPrefix;
            QueuePrefix = settings.QueuePrefix;
        }
    }

    /// <summary>
    /// 获取带前缀的表名
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <returns>带前缀的表名</returns>
    public static string GetTableName(string tableName) => $"{TablePrefix}{tableName}";

    /// <summary>
    /// 获取带前缀的缓存键
    /// </summary>
    /// <param name="key">缓存键</param>
    /// <returns>带前缀的缓存键</returns>
    public static string GetCacheKey(string key) => $"{CachePrefix}{key}";

    /// <summary>
    /// 获取带前缀的队列名
    /// </summary>
    /// <param name="queueName">队列名</param>
    /// <returns>带前缀的队列名</returns>
    public static string GetQueueName(string queueName) => $"{QueuePrefix}{queueName}";
}